package com.edu.internship.glabal.constants;

import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * code is far away from bug with the animal protecting
 *
 * @author lishiquan
 * @description
 * @date Create in 2018/3/16 10:52
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel("返回类-Result")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 4822942452184119123L;
    private String code;
    private String msg;
    private T data;

    private Result(ICodeEnum rCode, T data) {
        this.code = rCode.getCode();
        this.msg = rCode.getDesc();
        this.data = data;
    }

    public static Result<Void> success() {
        return new Result<>(ResultCodeEnum.SUCCESS, null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCodeEnum.SUCCESS, data);
    }

    public static <T> Result<T> success(ICodeEnum rCode, T data) {
        if (rCode == null) {
            return success(data);
        }
        return new Result<>(rCode, data);
    }

    public static <T> Result<T> failure(ICodeEnum rCode, T data) {
        return new Result<>(rCode, data);
    }
}
